package com.tedu.shoot.img;

import com.sun.prism.Image;
import sun.applet.Main;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * Date：2021年09月
 * Author：程Sir
 * Content:
 *  图片工具类，专门用于加载游戏图片
 */
public class Images {
    public static BufferedImage[] heroImage;
    public static BufferedImage skyImage;

    public static BufferedImage bulletImage;
    public static BufferedImage myImage;
    public static BufferedImage startImage;
    public static BufferedImage purseImage;
    public static BufferedImage gameOverImage;

    public static BufferedImage[] bigAirplaneImage;
    public static BufferedImage[] airplaneImage;
    public static BufferedImage[] beeImage;

    static  {
        /*
            java中异常处理
            1. 异常分为检查异常和非检查异常（运行时异常）
                检查异常：编译器进行了代码检查,需要做必要处理才可以
                运行异常：只有程序在运行时才会捕捉到的异常：NullPointException - RuntimeException
            2.针对检查异常的处理方式：
                （1）发现异常，内部自行消化，不需要让外部知道 ： try... catch...
                （2）逃避责任，直接交给上级（当前代码的调用者）进行处理： throws 异常名称
         */

        heroImage = new BufferedImage[2];
        heroImage[0] = readImage("hero0.png");
        heroImage[1] = readImage("hero1.png");

        bigAirplaneImage = new BufferedImage[5];
        airplaneImage = new BufferedImage[5];
        beeImage = new BufferedImage[5];

        bigAirplaneImage[0] = readImage("bigairplane.png");
        airplaneImage[0] = readImage("airplane.png");
        beeImage[0] = readImage("bee.png");
        for (int i = 1; i <5 ; i++) {
            bigAirplaneImage[i] = readImage("bom"+i+".png");
            airplaneImage[i] = readImage("bom"+i+".png");
            beeImage[i] = readImage("bom"+i+".png");
        }

        skyImage = readImage("background.png");
        bulletImage = readImage("bullet.png");
        myImage = readImage("my.png");

        startImage = readImage("start.png");
        purseImage = readImage("purse.png");
        gameOverImage = readImage("gameover.png");

    }

    /**
     * 读取和加载图片的方法
     * @param imageName 需要读取的图片名称
     * @return 返回读取到的图片
     */
    public static BufferedImage readImage(String imageName){
        BufferedImage image = null;
        try {
            // 使用的方式是加载URL 的方式而不是IO的方式
            image = ImageIO.read(Images.class.getResource(imageName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return image;
    }

    public static void main(String[] args) {
        new Images();
    }

}
